import 'package:aidou/model/user.dart';
import 'package:aidou/provider/user.dart';
import 'package:aidou/style/style.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

/// 其他手机号登录页
/// END
class OtherLoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _OtherLoginStatePage();
}

class _OtherLoginStatePage extends State<OtherLoginPage> {
  String userName = '';
  bool userIsEmpty = false;
  String password = '';
  bool passwordIsEmpty = false;
  final userController = TextEditingController();
  final pwdController = TextEditingController();

  @override
  void initState() {
    super.initState();

    userController.addListener(() {
      final user = userController.text;
      setState(() {
        userName = user;
        userIsEmpty = user != '';
      });
    });

    pwdController.addListener(() {
      final pwd = pwdController.text;
      setState(() {
        password = pwd;
        passwordIsEmpty = pwd != '';
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('其他手机号码登录'),
      ),
      body: Container(
        padding: EdgeInsets.all(40.w),
        width: double.infinity,
        height: double.infinity,
        color: ColorPlate.back2,
        child: Column(
          children: <Widget>[
            buildTextField(userController, '手机号', '请输入手机号码',
                Icons.supervisor_account, false),
            SizedBox(height: 60.h),
            buildTextField(
                pwdController, '验证码', '请输入验证码', Icons.lock_outline, true),
            SizedBox(height: 240.h),
            ElevatedButton(
              child: Text('登录'),
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colors.red),
                minimumSize: MaterialStateProperty.all(Size(580.w, 80.w)),
              ),
              onPressed: onPressLogin,
            ),
          ],
        ),
      ),
    );
  }

  /// 文本输入框
  Widget buildTextField(TextEditingController controller, String label,
      String hintText, IconData icon, bool obscure) {
    return TextField(
      controller: controller,
      cursorColor: Colors.white,
      style: TextStyle(
        fontSize: 36.sp,
        height: 1.5,
        color: Colors.white,
      ),
      obscureText: obscure,
      decoration: InputDecoration(
        hintText: hintText,
        hintStyle: TextStyle(color: Colors.white),
        filled: true,
        fillColor: ColorPlate.text,
        contentPadding: EdgeInsets.all(20.w),
        labelText: label,
        labelStyle: TextStyle(
          color: Colors.white,
        ),
        prefixIcon: Icon(
          icon,
          color: Colors.white,
        ),
        isCollapsed: true,
        border: OutlineInputBorder(
          borderSide: BorderSide(width: 0, color: Colors.transparent),
        ),
        focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(width: 0, color: Colors.transparent)),
      ),
      inputFormatters: [
        LengthLimitingTextInputFormatter(20),
      ],
    );
  }

  void onPressLogin() {
    UserModel user;
    if (!this.userIsEmpty || !this.passwordIsEmpty) {}
    print(this.userName);
    user = new UserModel(this.userName, this.password);
    context.read<UserProviderModel>().setUser(user);
  }
}
